package com.hippo.core.ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.VerticalDivider
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.model.LastVersion
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.ui.theme.PaddingMedium


@Composable
fun UpdateDialog(
    version: LastVersion,
    onDismissRequest: () -> Unit,
    onUpdate: () -> Unit,
) {

    val outsideDismissible = version.compulsory != 1

    Dialog(
        onDismissRequest = onDismissRequest,
        properties = androidx.compose.ui.window.DialogProperties(
            dismissOnBackPress = outsideDismissible,
            dismissOnClickOutside = outsideDismissible
        ),
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .background(Color.White, shape = MaterialTheme.shapes.medium )
                .padding(PaddingMedium)
        ) {

            if(outsideDismissible){
                Image(
                    painter = painterResource(id = R.mipmap.x),
                    contentDescription = "update",
                    modifier = Modifier
                        .align(Alignment.End)
                        .size(20.dp)
                        .click { onDismissRequest() }
                )
            }

            Text(
                text = stringResource(id = R.string.update_1),
                style = MaterialTheme.typography.titleLarge,
                textAlign = TextAlign.Center,
                modifier = Modifier.fillMaxWidth()
            )

            Spacer(modifier = Modifier.height(10.dp))

            Text(
                text = version.appVersionNo.orEmpty(),
                style = MaterialTheme.typography.bodySmall,
                textAlign = TextAlign.Center,
                modifier = Modifier.fillMaxWidth()
            )

            Spacer(modifier = Modifier.height(20.dp))

            Text(
                text = version.upgradeTips.orEmpty(),
                style = MaterialTheme.typography.bodyMedium,
            )

            Spacer(modifier = Modifier.height(20.dp))


            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.fillMaxWidth(),
            ) {

                Spacer(modifier = Modifier.weight(1f))


                TextButton(
                    onClick = onUpdate,
                ) {
                    Text(
                        text = stringResource(id = R.string.update_2),
                    )
                }


                if(version.compulsory != 1){

                    VerticalDivider(modifier = Modifier
                        .height(16.dp)
                        .padding(horizontal = 10.dp))


                    TextButton(
                        onClick = onDismissRequest,
                    ) {
                        Text(
                            text = stringResource(id = R.string.update_3),
                        )
                    }

                }

                Spacer(modifier = Modifier.weight(1f))
            }


        }
    }
}

@Preview(showBackground = true)
@Composable
private fun UpdateDialogPreview() {
    HippoTheme {
        UpdateDialog(
            version = LastVersion(
                appVersionNo = "1.0.0",
                compulsory = 1,
                downloadPath = "",
                isUpdate = 1,
                listAppMarket = "",
                newGrade = "",
                upgradeTips = "1.0.0版本更新"
            ),
            onDismissRequest = {},
            onUpdate = {}
        )
    }
}